import sys, os, glob, cv2
import os.path as osp
import numpy as np

file_path = os.path.abspath(__file__)
sys.path.append(os.path.abspath(os.path.join(file_path, "..", "..", "..")))
from code_aculat.utils.xml_process import analyze_xml
from code_aculat.visualize.visual_base import show_two_image


def get_image_including_target_cat():
    xml_dir = r"/Users/edy/Downloads/DP-192_Refine/Annotations"
    xml_files = glob.glob(osp.join(xml_dir, '*.xml'))

    target_xml = []
    target_cat = ["DP12 C310ml-油柑柠檬茶", ]
    for xml in xml_files:
        categories, _ = analyze_xml(xml)
        for cat in categories:
            if cat in target_cat:
                target_xml.append(osp.basename(xml))
                break

    for name_x in target_xml:
        print(name_x)


def compare_image_rectify():
    download_dir = r"/Users/edy/Downloads/东北饮料样品数据/"
    fold_1, fold_2 = os.listdir(download_dir)
    img_suffix = '.jpg'

    imgs = list(filter(lambda x: x.endswith(img_suffix), os.listdir(osp.join(download_dir, fold_2))))
    for img in imgs:
        f1_img = cv2.imdecode(np.fromfile(osp.join(download_dir, fold_1, img), dtype=np.uint8), flags=1)[:, :, ::-1]
        f2_img = cv2.imdecode(np.fromfile(osp.join(download_dir, fold_2, img), dtype=np.uint8), flags=1)[:, :, ::-1]
        show_two_image(f1_img, f2_img)
